#include <vector>
#include <iostream>
#include <map>
#include <sstream>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#include "HttpResponse.hh"




const std::string HttpResponse::APPLICATION_OCTET_STREAM="application/octet-stream";
const std::string HttpResponse::APPLICATION_JAVASCRIPT="application/javascript";
const std::string HttpResponse::TEXT_CSS="text/css";
const std::string HttpResponse::TEXT_HTML="text/html";
const std::string HttpResponse::IMAGE_JPEG="image/jpeg";
const std::string HttpResponse::IMAGE_X_ICON="image/x-icon";
const std::string HttpResponse::IMAGE_PNG="image/png";

//p
std::string HttpResponse::generator(){
/*
 * 		std::string version;
 * 		std::string status_code;
 * 		std::string reason_phrase;	
 * 		*/
	stringstream ss;
	ss<<" "<<version<<" "<<status_code<<" "<<reason_phrase<<"\r\n";
	
	map<std::string,std::string>::iterator it;
	for(it=headers.begin();it!=headers.end();it++){
		ss<<it->first<<": "<<it->second<<"\r\n";
	}

	ss<<"\r\n";
	//ss<<content;
	
	return ss.str();
}

void HttpResponse::setHead(const std::string & key,const std::string &val){
	this->headers.insert(std::pair<std::string,std::string>(key,val));
}

/*
 *  * HTTP/1.1 200 OK
 *   *
 *    * */
HttpResponse::HttpResponse(){
	this->version="HTTP/1.1";
	this->status_code="200";
	this->reason_phrase="OK";
	//Content-Length: 122
	
}
void HttpResponse::setContentType(std::string type){
	setHead("Content-Type",type);
}

void HttpResponse::setContentLength(size_t len){
	char buffer[20]="";
	sprintf(buffer,"%ld",len);
	setHead("Content-Length",string(buffer));
}


bool HttpResponse::setFile(const std::string &filename){
	sendfiles.push_back(filename);
}


bool HttpResponse::setTxt(const std::string &filename){
	setContentType("text/html;charset=GBK");
	setFile(filename);
}


